<?php


namespace app\api\controller;



use app\api\model\Article;
use app\api\model\Listening;
use app\api\model\Music;
use app\api\model\News;
use app\api\model\NewsCollect;
use app\api\model\NewsLike;
use app\api\model\WordCollect;
use app\framework\BaseController;
use app\framework\DateUtils;
use app\framework\Utils;
use think\Db;
use think\Exception;

class user extends BaseController
{

    /**
     * 返回当前用户收藏的单词
     */
    public function queryCollectWordForUser(){
        try{
            $openId = parent::getUserOpenId();
            $list = Db::query('SELECT t.id,word.word,word.translate,word.id AS wordId,true AS showlike FROM t_word_collect t LEFT JOIN t_word word ON word.id = t.word_id WHERE t.open_id="'.$openId.'"');
            parent::writeJSON($list,parent::MSG_OK,parent::SC_FAIL);
        }catch (Exception $e){
            parent::writeJSON(null,$e->getMessage(),parent::SC_FAIL);
        }
    }

    /**
     * 显示指定字母前缀的所有单词
     * @param $prefix 单词前缀字母
     */
    public function queryAllWord($prefix = null){
        try{
            $openId = parent::getUserOpenId();
            $list = Db::query('SELECT t.*,IF( tc.id IS NULL, FALSE, TRUE ) AS showlike FROM t_word t LEFT JOIN t_word_collect tc ON tc.word_id = t.id AND tc.open_id = "'.$openId.'" WHERE t.word LIKE "'.$prefix.'%"');
            parent::writeJSON($list,parent::MSG_OK,parent::SC_FAIL);
        }catch (Exception $e){
            parent::writeJSON(null,$e->getMessage(),parent::SC_FAIL);
        }
    }

    /**
     * 收藏单词
     * @param $wordId 单词ID
     */
    public function collectWord($wordId){
        try{
            if(empty($wordId)){
                throw new Exception('请选择收藏的单词');
            }
            $wordCollect = new WordCollect();
            // 判断是否已经收藏
            $count = $wordCollect
                ->where('word_id',$wordId)
                ->where('open_id',parent::getUserOpenId())
                ->count();
            if($count == 0){
                // 未收藏
                $wordCollect->save([
                    'id'          => Utils::create_guid(),
                    'word_id'     => $wordId,
                    'open_id'     => parent::getUserOpenId(),
                    'create_date' => DateUtils::dateFormate()
                ]);
            }else{
                // 已收藏，取消收藏
                $wordCollect::destroy(['word_id'=>$wordId,'open_id'=>parent::getUserOpenId()]);
            }
            parent::writeJSON(null,parent::MSG_OK,parent::SC_FAIL);
        }catch (Exception $e){
            parent::writeJSON(null,$e->getMessage(),parent::SC_FAIL);
        }
    }

    /**
     * 查询文章列表
     */
    public function queryArticleList(){
        try{
           $list = Db::query('SELECT * FROM t_article');
            parent::writeJSON($list,parent::MSG_OK,parent::SC_FAIL);
        }catch (Exception $e){
            parent::writeJSON(null,$e->getMessage(),parent::SC_FAIL);
        }
    }

    /**
     * 查询指定文章详细信息
     * @param null $id
     */
    public function queryArticle($id = null){
        try{
            if(empty($id)){
                throw new Exception('文章ID不可为空');
            }
            $data = Article::get(['id' => $id]);
            parent::writeJSON($data,parent::MSG_OK,parent::SC_FAIL);
        }catch (Exception $e){
            parent::writeJSON(null,$e->getMessage(),parent::SC_FAIL);
        }
    }

    /**
     * 查询头条新闻列表
     */
    public function queryNewsList(){
        try{
            $openId = parent::getUserOpenId();
            $list = Db::query('SELECT t.*,IF( tc.id IS NULL, FALSE, TRUE ) AS isCollect,IF( tl.id IS NULL, FALSE, TRUE ) AS isLike FROM t_news t LEFT JOIN t_news_collect tc ON tc.news_id = t.id AND tc.open_id = "'.$openId.'"
	LEFT JOIN t_news_like tl ON tl.news_id = t.id AND tl.open_id = "'.$openId.'"');
            parent::writeJSON($list,parent::MSG_OK,parent::SC_FAIL);
        }catch (Exception $e){
            parent::writeJSON(null,$e->getMessage(),parent::SC_FAIL);
        }
    }

    /**
     * 查询指定新闻详细信息
     * @param null $id
     */
    public function queryNews($id = null){
        try{
            if(empty($id)){
                throw new Exception('新闻ID不可为空');
            }
            $openId = parent::getUserOpenId();
            $data = Db::query('SELECT t.*,IF( tc.id IS NULL, FALSE, TRUE ) AS isCollect,IF( tl.id IS NULL, FALSE, TRUE ) AS isLike FROM t_news t LEFT JOIN t_news_collect tc ON tc.news_id = t.id AND tc.open_id = "'.$openId.'"
	LEFT JOIN t_news_like tl ON tl.news_id = t.id AND tl.open_id = "'.$openId.'" WHERE t.id="'.$id.'"');
            parent::writeJSON($data,parent::MSG_OK,parent::SC_FAIL);
        }catch (Exception $e){
            parent::writeJSON(null,$e->getMessage(),parent::SC_FAIL);
        }
    }

    /**
     * 收藏新闻
     * @param $id
     */
    public function collectNews($id){
        try{
            if(empty($id)){
                throw new Exception('请选择收藏的新闻文章');
            }
            $newsCollect = new NewsCollect();
            // 判断是否已经收藏
            $count = $newsCollect
                ->where('news_id',$id)
                ->where('open_id',parent::getUserOpenId())
                ->count();
            if($count == 0){
                // 未收藏
                $newsCollect->save([
                    'id'          => Utils::create_guid(),
                    'news_id'     => $id,
                    'open_id'     => parent::getUserOpenId(),
                    'create_date' => DateUtils::dateFormate()
                ]);
            }else{
                // 已收藏，取消收藏
                $newsCollect::destroy(['news_id'=>$id,'open_id'=>parent::getUserOpenId()]);
            }
            parent::writeJSON(null,parent::MSG_OK,parent::SC_FAIL);
        }catch (Exception $e){
            parent::writeJSON(null,$e->getMessage(),parent::SC_FAIL);
        }
    }

    /**
     * 点赞新闻
     * @param $id
     */
    public function likeNews($id){
        try{
            if(empty($id)){
                throw new Exception('请选择点赞的新闻文章');
            }
            $newsLike = new NewsLike();
            // 判断是否已经收藏
            $count = $newsLike
                ->where('news_id',$id)
                ->where('open_id',parent::getUserOpenId())
                ->count();
            if($count == 0){
                // 未收藏
                $newsLike->save([
                    'id'          => Utils::create_guid(),
                    'news_id'     => $id,
                    'open_id'     => parent::getUserOpenId(),
                    'create_date' => DateUtils::dateFormate()
                ]);
            }else{
                // 已点赞，取消点赞
                $newsLike::destroy(['news_id'=>$id,'open_id'=>parent::getUserOpenId()]);
            }
            parent::writeJSON(null,parent::MSG_OK,parent::SC_FAIL);
        }catch (Exception $e){
            parent::writeJSON(null,$e->getMessage(),parent::SC_FAIL);
        }
    }

    /**
     * 获取收藏的新闻
     */
    public function queryCollectNewsList(){
        try{
            $openId = parent::getUserOpenId();
            $list = Db::query('SELECT t.*,true AS isCollect FROM t_news_collect tc LEFT JOIN t_news t ON t.id = tc.news_id WHERE tc.open_id = "'.$openId.'"');
            parent::writeJSON($list,parent::MSG_OK,parent::SC_FAIL);
        }catch (Exception $e){
            parent::writeJSON(null,$e->getMessage(),parent::SC_FAIL);
        }
    }

    /**
     * 获取点赞的新闻
     */
    public function queryLikeNewsList(){
        try{
            $openId = parent::getUserOpenId();
            $list = Db::query('SELECT t.*,true AS isLike FROM t_news_like tl LEFT JOIN t_news t ON t.id = tl.news_id WHERE tl.open_id = "'.$openId.'"');
            parent::writeJSON($list,parent::MSG_OK,parent::SC_FAIL);
        }catch (Exception $e){
            parent::writeJSON(null,$e->getMessage(),parent::SC_FAIL);
        }
    }

    /**
     * 获取每日英语
     * 注意：目前取时间最新的一条
     */
    public function getStickers(){
        try{
            $data = Db::query('SELECT * FROM t_stickers ORDER BY create_date desc LIMIT 0,1');
            parent::writeJSON($data,parent::MSG_OK,parent::SC_FAIL);
        }catch (Exception $e){
            parent::writeJSON(null,$e->getMessage(),parent::SC_FAIL);
        }
    }

    /**
     * 音乐列表
     */
    public function queryMusicList(){
        try{
            $music = new Music();
            $list = $music->select();
            parent::writeJSON($list,parent::MSG_OK,parent::SC_FAIL);
        }catch (Exception $e){
            parent::writeJSON(null,$e->getMessage(),parent::SC_FAIL);
        }
    }

    /**
     * 获取所有听力列表
     */
    public function queryListening(){
        try{
            $listening = new Listening();
            $list = $listening->select();
            parent::writeJSON($list,parent::MSG_OK,parent::SC_FAIL);
        }catch (Exception $e){
            parent::writeJSON(null,$e->getMessage(),parent::SC_FAIL);
        }
    }
}